

/**
 * Componentes de YUI (DataTable)
 * 
 * @author Carlos Enrique Peña Albarran
 * @version 1.0.0
 * @package SaGem.system.js.nomina
 */



YAHOO.util.Event.addListener(window, "load", function() {
	var Ex = YAHOO.namespace('example');

	/**
	 * Objetos DataSource
	 *
	 */
	
	Ex.dataSource = new YAHOO.util.DataSource(SaGem.empleado, {
		responseType : YAHOO.util.DataSource.TYPE_JSARRAY,
		responseSchema : SaGem.esquema,
		doBeforeCallback : function(req, raw, res, cb) {
			// This is the filter function
		var data = res.results || [], filtered = [], i, l;
		if (req) {
			req = req.toLowerCase();
			for (i = 0, l = data.length; i < l; ++i) {
				if (!data[i].nombre.toLowerCase().indexOf(req)) {
					filtered.push(data[i]);
				}
			}
			res.results = filtered;
		}

		return res;
	}
	});

	/*
	 * Control de Paginas
	 * 
	 */
	Ex.paginator = new YAHOO.widget.Paginator( {
		rowsPerPage : 13,
		pageLinks : 5

	});

	Ex.conf = {
		paginator : Ex.paginator,
		sortedBy : {
			key : 'codigo',
			dir : YAHOO.widget.DataTable.CLASS_ASC
		}
	};

	/*
	 * Tabla de datos
	 */


	Ex.dataTable = new YAHOO.widget.DataTable('tbl', SaGem.definicion, Ex.dataSource,
			Ex.conf, {
				scrollable : false,
				height : "300"
			});

	Ex.filterTimeout = null;
	Ex.updateFilter = function() {
		// Reset timeout
		Ex.filterTimeout = null;

		// Reset sort
		var cedula = Ex.dataTable.getState();
		cedula.sortedBy = {
			key : 'codigo',
			dir : YAHOO.widget.DataTable.CLASS_ASC
		};

		// Get filtered data
		Ex.dataSource.sendRequest(YAHOO.util.Dom.get('filter').value, {
			success : Ex.dataTable.onDataReturnInitializeTable,
			failure : Ex.dataTable.onDataReturnInitializeTable,
			scope : Ex.dataTable,
			argument : cedula
		});

	};

	YAHOO.util.Event.on('filter', 'keyup', function(e) {
		clearTimeout(Ex.filterTimeout);
		setTimeout(Ex.updateFilter, 600);
	});

	// Eventos de Seleccion del objeto
		Ex.dataTable.subscribe("rowMouseoverEvent",
				Ex.dataTable.onEventHighlightRow);
		Ex.dataTable.subscribe("rowMouseoutEvent",
				Ex.dataTable.onEventUnhighlightRow);
		Ex.dataTable.subscribe("rowClickEvent", Ex.dataTable.onEventSelectRow);

		Ex.dataTable.on('cellClickEvent', function(oArgs) {
			// 

				var target = oArgs.target;
				var record = this.getRecord(target);
				var column = this.getColumn(target);
				var sexo = "";
				var codigo;
				var tiponomina = "";
				var estado_civil = "";
				
				
				sexo = "FEMENINO";
				if (record.getData('sexo') != "F") {
					sexo = "MASCULINO";
				}
				var x = document.all.frmPersona;

				document.getElementById("lblCodigo").innerHTML = record.getData('codigo');
				document.getElementById("lblCedula").innerHTML = record.getData('cedula');
				document.getElementById("lblNombres").innerHTML = record.getData('nombre') + ' ' +  record.getData('nombre2');
				document.getElementById("lblApellidos").innerHTML = record.getData('apellido') + ' ' + record.getData('apellido2');
				document.getElementById("lblDireccion").innerHTML = record.getData('direccion');
				document.getElementById("lblTelefono").innerHTML = record.getData('telefono');
				document.getElementById("lblCargo").innerHTML = record.getData('cargo');
				document.getElementById("lblFecha_Nacimiento").innerHTML =	record.getData('dia')+ "-" + record.getData('mes') + "-" + record.getData('ano');

				
				/* Formularios */
				document.getElementById("txtCodigo_Nomina").value = record.getData('codigo');
				document.getElementById("txtNombreBanco").value = record.getData('banco');
				document.getElementById("txtNumeroCta_Bancaria").value = record.getData('cuenta');
				document.getElementById("txtCedula").value = record.getData('cedula');
				document.getElementById("txtNombre1").value = record.getData('nombre');
				document.getElementById("txtNombre2").value = record.getData('nombre2');
				document.getElementById("txtApellido1").value = record.getData('apellido');
				document.getElementById("txtApellido2").value = record.getData('apellido2');
				document.getElementById("txtCargo").value = record.getData('cargo');
				document.getElementById("txtCiudad").value = record.getData('ciudad');
				document.getElementById("txtDireccion").value = record.getData('direccion');
				document.getElementById("txtTipo_Nomina").value = record.getData('destiponomina');				
				document.getElementById("txtTelefono").value = record.getData('telefono');				
				document.getElementById("txtDiaNacimiento").value = record.getData('dia');						
				document.getElementById("txtMesNacimiento").value = record.getData('mes');						
				document.getElementById("txtAnoNacimiento").value = record.getData('ano');
				
					
				
				document.getElementById("txtSexo").value = sexo;
				document.getElementById("txtEdocivil").value = record.getData('edocivil');
				document.getElementById("txtNivelAcademico").value = record.getData('nivelacademico');
				
				document.getElementById("txtDiaIngreso").value = record.getData('diaing');						
				document.getElementById("txtMesIngreso").value = record.getData('mesing');						
				document.getElementById("txtAnoIngreso").value = record.getData('anoing');
				
				
//				document.getElementById("txtTipo_Nomina").value = tiponomina;
				document.images["imgFotoAlumno"].src = "http://localhost/SaGem/system/img/fotos/" + record.getData('cedula') + "/" + record.getData('cedula') + ".jpg";
				
				//$("#dtHistorial").load("");
				var strUrl = "";
				if(record.getData('tiponomina') == "OS"){
					strUrl = "http://localhost/SaGem/index.php/sagem/historial_pagos/" + record.getData('codigo') + "/Sem.01-02/Catorcenal/" + record.getData('tiponomina');
				}else{
					strUrl = "http://localhost/SaGem/index.php/sagem/historial_pagos/" + record.getData('codigo') + "/Agosto/Mensual/" + record.getData('tiponomina');
				}
				//document.getElementById("dtHistorial").innerHTML = "";
				new Ajax.Updater('dtHistorial', strUrl);
				YAHOO.example.container.dtpersonales.show();

		});
		
		Ex.dataTable.on('cellDblclickEvent', function(oArgs) {
				//			YAHOO.example.container.dtpersonales.show();

		});
	});
